#!/bin/sh
#-----------------------------------------------------------------------------
# Program:   Insight Segmentation & Registration Toolkit
# Module:    $RCSfile: itkwish.in,v $
# Language:  C++
# Date:      $Date: 2006/09/06 20:58:42 $
# Version:   $Revision: 1.1 $
#
# Copyright (c) Insight Software Consortium. All rights reserved.
# See ITKCopyright.txt or http://www.itk.org/HTML/Copyright.htm for details.
#
# Portions of this code are covered under the VTK copyright.
# See VTKCopyright.txt or http://www.kitware.com/VTKCopyright.htm for details.
#
#    This software is distributed WITHOUT ANY WARRANTY; without even 
#    the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
#    PURPOSE.  See the above copyright notices for more information.
#-----------------------------------------------------------------------------

#
# This is a shell script driving the ITK Tcl wrapper executable.  It
# sets up the environment and then executes the real executable.
# Alternatively, if the first command line argument is "--run", the rest
# of the command line will be invoked as a command in the proper
# environment.
#
# The real executable is located relative to this script in
#   ../lib/InsightToolkit
#

ITKWISH_Usage()
{
  echo "Insight Segmentation & Registration Toolkit (http://www.itk.org)"
  echo "This is the Tcl wrapper executable driver."
  echo ""
  echo "Usage:"
  echo "  itkwish           = Run itkwish interactively as a Tcl interpreter."
  echo "  itkwish foo.tcl   = Run \"foo.tcl\" in the ITK Tcl interpreter."
  echo "  itkwish --run ... = Run command \"...\" in the itkwish environment."
  echo ""
  echo "Example commands:"
  echo "  \"itkwish\""
  echo "    Provides a tcl prompt "%" from which ITK scripts can be written"
  echo "    interactively."
  echo ""
  echo "  \"itkwish myITKScript.tcl\""
  echo "    Runs the myITKScript.tcl script in the ITK Tcl interpreter.."
  echo ""
  echo "  \"itkwish --run wish\""
  echo "    Runs wish in an environment in which the ITK packages can be"
  echo "    loaded with \"package require InsightToolkit\"."
}


if [ -d "@WRAP_ITK_TCL_DIR@" ]; then : ; else
  ITKWISH_Usage
  echo ""
  echo "Error:"
  echo "  This script is meant to be used from an itk installation directory."
  echo "  It will not run from the itk source tree."
  exit 1
fi


# Setup environment.
case "`uname`" in
Darwin)
  if [ -z "$DYLD_LIBRARY_PATH" ]; then
    export DYLD_LIBRARY_PATH="@WRAP_ITK_TCL_SWIG_DIR@"
  else
    export DYLD_LIBRARY_PATH="@WRAP_ITK_TCL_SWIG_DIR@:$DYLD_LIBRARY_PATH"
  fi
  ;;
*)
  if [ -z "$LD_LIBRARY_PATH" ]; then
    export LD_LIBRARY_PATH="@WRAP_ITK_TCL_SWIG_DIR@"
  else
    export LD_LIBRARY_PATH="@WRAP_ITK_TCL_SWIG_DIR@:$LD_LIBRARY_PATH"
  fi
esac

if [ -z "$TCLLIBPATH" ]; then
  export TCLLIBPATH="\"@WRAP_ITK_TCL_DIR@\""
else
  export TCLLIBPATH="\"@WRAP_ITK_TCL_DIR@\" $TCLLIBPATH"
fi

if [ "$1" = "--help" ]; then
  # Display usage.
  ITKWISH_Usage
  exit
elif [ "$1" = "--run" ]; then
  # Skip the "--run" argument.
  shift
  # Run exact command line given after "--run" in this environment.
  exec "$@"
else
  # Run real itkwish executable in this environment.
  exec "@WRAP_ITK_TCL_ITKWISH_DIR@/itkwish" "$@"
fi
